#include <PA9.h>
#include "palette.h"

namespace game{
    u8 Palette::maxPaletteId0 = 0;
    u8 Palette::maxPaletteId1 = 0;
        
    u8 Palette::nextPalette(bool screen){
        if(screen){
            return maxPaletteId1++;
        }
        else{
            return maxPaletteId0++;
        }
    }
    
    Palette::Palette(bool screen, void* objPalette){
        Palette::screen = screen;
        Palette::objPalette = objPalette;
        Palette::paletteid = -1;
    }
    
    u8 Palette::getid(){
        return paletteid;
    }
    
    u8 Palette::getdualid(){
        return dualpaletteid;
    }
    
    void Palette::load(){
        if(!loaded){
            paletteid = Palette::nextPalette(screen);
            PA_LoadSpritePal(screen, paletteid, objPalette);
            loaded = true;
        }
    }
    
    void Palette::loaddual(){
        if(!loaded){
            u8 id[2];
            id[false] = Palette::nextPalette(false);
            id[true] = Palette::nextPalette(true);
            
            if(id[true] == id[false])
                PA_DualLoadSpritePal(id[true], objPalette);
            else{
                maxPaletteId1--;
                maxPaletteId0--;
                return;
            }
                
            dualpaletteid = id[true];
            
            loaded = true;
        }   
    }
    
    bool Palette::isloaded(){
        return loaded;
    }
    
    void Palette::setBgColor(u16 color){
        PA_DualSetBgColor(color);
    }
    
    s16 Palette::getcolor(u8 r, u8 g, u8 b){
        return PA_RGB(r,g,b);
    }
}
